#pragma once
#include "DirectXFramework.h"

void CDirectXFramework::Render()
{
	// If the device was not created successfully, return
	if(!m_pD3DDevice)
		return;



	//////////////////////////////////////////////////////////////////////////
	// All draw calls between swap chain's functions, and pre-render and post- 
	// render functions (Clear and Present, BeginScene and EndScene)
	//////////////////////////////////////////////////////////////////////////

	// Clear the back buffer, call BeginScene()
	if(SUCCEEDED(m_pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET, D3DXCOLOR(0.0f, 0.4f, 0.0f, 0.0f), 1.0f, 0)))
	{
		if(SUCCEEDED(m_pD3DDevice->BeginScene()))
		{
			//////////////////////////////////////////////////////////////////////////
			// Draw 3D Objects
			//////////////////////////////////////////////////////////////////////////

			//////////////////////////////////////////////////////////////////////////
			// Draw 2D sprites
			//////////////////////////////////////////////////////////////////////////
			// Note: You should only be calling the sprite object's begin and end once, 
			// with all draw calls of sprites between them

			// Call Sprite's Begin to start rendering 2D sprite objects
			if(SUCCEEDED(m_pD3DSprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_DEPTH_FRONTTOBACK)))
			{
				//////////////////////////////////////////////////////////////////////////
				// Matrix Transformations to control sprite position, scale, and rotate
				// Set these matrices for each object you want to render to the screen
				//////////////////////////////////////////////////////////////////////////
				D3DXMATRIX transMat, rotMat, scaleMat, worldMat;
				D3DXMatrixIdentity(&transMat);
				D3DXMatrixIdentity(&scaleMat);
				D3DXMatrixIdentity(&rotMat);
				D3DXMatrixIdentity(&worldMat);
				//case statement for start**********
				switch(menuState)
				{
				case Main:
					D3DXMatrixScaling(&scaleMat, 0.8f, 0.8f, 0.0f);			// Scaling
					//D3DXMatrixRotationZ(&rotMat, deck.rotation);	// Adjust speed of rotation by scaling the time value by an amount
					D3DXMatrixTranslation(&transMat, 400.0f, 150.0f, 0.0f);	// Translation
					//D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat);		// Multiply scale and rotation, store in scale
					D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat);	// Multiply scale and translation, store in world

					// Set Transform
					m_pD3DSprite->SetTransform(&worldMat);

					// Draw the texture with the sprite object
					m_pD3DSprite->Draw(texture, 0, &D3DXVECTOR3(imageInfo.Width * 0.5f, imageInfo.Height * 0.5f, 0.0f),
						0, D3DCOLOR_ARGB(255,255,255,255));
					break;
				case Start:
					//Draw deck
					D3DXMatrixScaling(&scaleMat, deck.scale, deck.scale, 0.0f);			// Scaling
					D3DXMatrixRotationZ(&rotMat, deck.rotation);	// Adjust speed of rotation by scaling the time value by an amount
					D3DXMatrixTranslation(&transMat, deck.position.x, deck.position.y, 0.0f);	// Translation
					D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat);		// Multiply scale and rotation, store in scale
					D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat);	// Multiply scale and translation, store in world

					// Set Transform
					m_pD3DSprite->SetTransform(&worldMat);

					// Draw the texture with the sprite object
					m_pD3DSprite->Draw(m_pTexture[0], 0, &D3DXVECTOR3(m_imageInfo[0].Width * 0.5f, m_imageInfo[0].Height * 0.5f, 0.0f),
						0, deck.color);

					//Draw Com's cards
					for(int i = 0; i < 5; ++i)
					{
						D3DXMatrixScaling(&scaleMat, com.GetCard(i).scale, 0.7f, 0.0f);			// Scaling
						D3DXMatrixRotationZ(&rotMat, com.GetCard(i).rotation);	// Rotation on Z axis, value in radians, converting from degrees
						D3DXMatrixTranslation(&transMat, com.GetCard(i).position.x, com.GetCard(i).position.y, 0.0f);	// Translation
						D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat);		// Multiply scale and rotation, store in scale
						D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat);	// Multiply scale and translation, store in world

						// Set Transform
						m_pD3DSprite->SetTransform(&worldMat);

						// Draw the texture with the sprite object
						if(com.GetCard(i).faceup)
						{
							m_pD3DSprite->Draw(m_pTexture[com.GetCard(i).value], 0, &D3DXVECTOR3(m_imageInfo[com.GetCard(i).value].Width * 0.5f, m_imageInfo[com.GetCard(i).value].Height * 0.5f, 0.0f),
								0, com.GetCard(i).color);

							// draw the suit over it
							m_pD3DSprite->Draw(suitTexture[com.GetCard(i).suit], 0, &D3DXVECTOR3(suitimageInfo[com.GetCard(i).suit].Width * 0.5f, suitimageInfo[com.GetCard(i).suit].Height * 0.5f, 0.0f),
								0, com.GetCard(i).color);
						}
						else
							m_pD3DSprite->Draw(m_pTexture[0], 0, &D3DXVECTOR3(m_imageInfo[0].Width * 0.5f, m_imageInfo[0].Height * 0.5f, 0.0f),
							0, com.GetCard(i).color);
					}

					//Draw Hmn's cards
					for(int i = 0; i < 5; ++i)
					{
						D3DXMatrixScaling(&scaleMat, hmn.GetCard(i).scale, 0.7f, 0.0f);			// Scaling
						D3DXMatrixRotationZ(&rotMat, hmn.GetCard(i).rotation);	// Rotation on Z axis, value in radians, converting from degrees
						D3DXMatrixTranslation(&transMat, hmn.GetCard(i).position.x, hmn.GetCard(i).position.y, 0.0f);	// Translation
						D3DXMatrixMultiply(&scaleMat, &scaleMat, &rotMat);		// Multiply scale and rotation, store in scale
						D3DXMatrixMultiply(&worldMat, &scaleMat, &transMat);	// Multiply scale and translation, store in world

						// Set Transform
						m_pD3DSprite->SetTransform(&worldMat);

						// Draw the texture with the sprite object
						if(hmn.GetCard(i).faceup)
						{
							m_pD3DSprite->Draw(m_pTexture[hmn.GetCard(i).value], 0, &D3DXVECTOR3(m_imageInfo[hmn.GetCard(i).value].Width * 0.5f, m_imageInfo[hmn.GetCard(i).value].Height * 0.5f, 0.0f),
								0, hmn.GetCard(i).color);

							// draw the suit over it
							m_pD3DSprite->Draw(suitTexture[hmn.GetCard(i).suit], 0, &D3DXVECTOR3(suitimageInfo[hmn.GetCard(i).suit].Width * 0.5f, suitimageInfo[hmn.GetCard(i).suit].Height * 0.5f, 0.0f),
								0, hmn.GetCard(i).color);
						}
						else
							m_pD3DSprite->Draw(m_pTexture[0], 0, &D3DXVECTOR3(m_imageInfo[0].Width * 0.5f, m_imageInfo[0].Height * 0.5f, 0.0f),
							0, hmn.GetCard(i).color);
					}
					//start statement end for render
					break;
				default:
					break;
				}
				// End drawing 2D sprites
				m_pD3DSprite->End();
			}

			//////////////////////////////////////////////////////////////////////////
			// Draw Text
			//////////////////////////////////////////////////////////////////////////

			// Calculate RECT structure for text drawing placement, using whole screen
			RECT rect;
			GetWindowRect(m_hWnd, &rect);
			rect.right = rect.right - rect.left;
			rect.bottom = rect.bottom - rect.top;
			rect.left = 0;
			rect.top = 0;
			switch(menuState)
			{
			case Main:
				m_pD3DFont->DrawText(0, L"Press S to Start game\n\n\n\n\n", -1, &rect, 
					DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				m_pD3DFont->DrawText(0, L"Press O for Options\n\n\n\n", -1, &rect, 
					DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				break;
			case Option:
				m_pD3DFont->DrawText(0, L"Uses the basic rules of Poker\nPress 1-5 to select a card and Enter when you're ready\nBest 5 of 9 wins", -1, &rect, 
					DT_TOP | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				m_pD3DFont->DrawText(0, L"\n\n\n\nTechnical Lead: Jordon Vaughn\nGeneral Lead: David DJ Jackson\nComputer AI: Michael Carpenter", -1, &rect, 
					DT_TOP | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				m_pD3DFont->DrawText(0, L"Press B to return to Main menu\n\n\n", -1, &rect, 
					DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				break;
			case Start:
				// Draw Text, using DT_TOP, DT_RIGHT for placement in the top right of the
				// screen.  DT_NOCLIP can improve speed of text rendering, but allows text
				// to be drawn outside of the rect specified to draw text in.

				if (step == Select)
					m_pD3DFont->DrawText(0, L"1-5 to\nselect\n\nEnter\nto swap\n\n\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				if (step == Show)
					m_pD3DFont->DrawText(0, L"Comparing\nHands\n\nEnter to\ncontinue\n\n\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				//Draw hmn hand type
				if (step == Select || step == Show || step == Win)
					m_pD3DFont->DrawText(0, hmn.Check(), -1, &rect, DT_BOTTOM | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				else
					m_pD3DFont->DrawText(0, L"?????", -1, &rect, DT_BOTTOM | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				//Draw com hand type
				if (!hide)
					m_pD3DFont->DrawText(0, com.Check(), -1, &rect, DT_TOP | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				else
					m_pD3DFont->DrawText(0, L"?????", -1, &rect, DT_TOP | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				if (step == Win)
				{
					if (winner == 0)
						m_pD3DFont->DrawText(0, L"It's a\nDraw\n\nEnter\nto retry\n\n\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					if (winner == 1)
						m_pD3DFont->DrawText(0, L"You\nWin\n\nEnter\nto retry\n\n\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					if (winner == 2)
						m_pD3DFont->DrawText(0, L"You\nLose\n\nEnter\nto retry\n\n\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				}

				// Draw FPS
				wchar_t buffer[64];
				swprintf_s(buffer, 64, L"FPS: %d", m_FPS);
				m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_TOP | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				swprintf_s(buffer, 64, L"Win:   %d\n ", win);
				m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				swprintf_s(buffer, 64, L"Lose: %d", loss);
				m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

				if (step == Select || step == Show || step == Win)
				{
					int hmnhigh = hmn.HighCard();

					if(hmnhigh <= 10)
					{
						swprintf_s(buffer, 64, L"Hmn High: %d\n\n\n\n", hmn.HighCard());
						m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					}
					else if(hmnhigh == 11)
						m_pD3DFont->DrawText(0, L"Hmn High: 11 J\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(hmnhigh == 12)
						m_pD3DFont->DrawText(0, L"Hmn High: 12 Q\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(hmnhigh == 13)
						m_pD3DFont->DrawText(0, L"Hmn High: 13 K\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(hmnhigh == 14)
						m_pD3DFont->DrawText(0, L"Hmn High: 14 A\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				}
				else
				{
					m_pD3DFont->DrawText(0, L"Hmn High: ?\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				}

				if (!hide)
				{
					int comhigh = com.HighCard();

					if(comhigh <= 10)
					{
						swprintf_s(buffer, 64, L"Com High: %d\n\n\n\n\n", comhigh);
						m_pD3DFont->DrawText(0, buffer, -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					}
					else if(comhigh == 11)
						m_pD3DFont->DrawText(0, L"Com High: 11 J\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(comhigh == 12)
						m_pD3DFont->DrawText(0, L"Com High: 12 Q\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(comhigh == 13)
						m_pD3DFont->DrawText(0, L"Com High: 13 K\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
					else if(comhigh == 14)
						m_pD3DFont->DrawText(0, L"Com High: 14 A\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				}
				else
				{
					m_pD3DFont->DrawText(0, L"Com High: ?\n\n\n\n\n", -1, &rect, DT_BOTTOM | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));
				}
				break;
			default:
				break;
			}
			// EndScene, and Present the back buffer to the display buffer
			m_pD3DDevice->EndScene();
		}

	}
	m_pD3DDevice->Present(0, 0, 0, 0);
}

//// Calculate Frames per Second
//m_currTime = (float)timeGetTime();
//static int fpsCounter = 0;
//if(m_currTime - m_prevTime >= 1000.0f)
//{
//	m_prevTime = m_currTime;
//	m_FPS = fpsCounter;
//	fpsCounter = 0;		
//}
//else
//{
//	++fpsCounter;
//}
//
//}